RestApiService   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 52
dl 0
loc 81
rs 10
c 0
b 0
f 0

6 Functions

Rating   Name   Duplication   Size   Complexity  
A createProduct 0 7 1
A deleteProduct 0 9 1
A handleError 0 13 2
A updateProduct 0 7 1
A getProduct 0 7 1
A getProducts 0 7 1
1
import { Injectable } from '@angular/core';
2
import { HttpClient, HttpHeaders } from '@angular/common/http';
3
import { Product } from '../models/product';
4
import { Observable, throwError } from 'rxjs';
5
import { retry, catchError } from 'rxjs/operators';
6
import { environment } from "./../../environments/environment";
7
8
@Injectable({
9
    providedIn: 'root'
10
})
11
12
export class RestApiService {
13
14
    // Define API
15
    apiURL = `${environment.api}`;
16
17
    constructor(private http: HttpClient) { }
18
19
    // Http Options
20
    httpOptions = {
21
        headers: new HttpHeaders({
22
            'Access-Control-Allow-Origin':'*',
23
            'Content-Type': 'application/json',
24
            'Accept': 'application/json',
25
            'Access-Control-Allow-Methods': 'GET, POST, PATCH, PUT, DELETE, OPTIONS'
26
        })
27
    }
28
29
  // HttpClient API get() method => Fetch products list
30
    getProducts(): Observable<Product> {
31
        return this.http.get<Product>(this.apiURL + 'products' + "?API_KEY="+environment.api_key)
32
        .pipe(
33
            retry(1),
34
            catchError(this.handleError)
35
        )
36
    }
37
38
  // HttpClient API get() method => Fetch product
39
    getProduct(id: any): Observable<Product> {
40
        return this.http.get<Product>(this.apiURL + 'products/' + id +"?API_KEY="+environment.api_key)
41
        .pipe(
42
            retry(1),
43
            catchError(this.handleError)
44
        )
45
    }
46
47
    // HttpClient API post() method => Create product
48
    createProduct(product: any): Observable<Product> {
49
        return this.http.post<Product>(this.apiURL + 'products', JSON.stringify(product), this.httpOptions)
50
        .pipe(
51
            retry(1),
52
            catchError(this.handleError)
53
        )
54
    }
55
56
    // HttpClient API put() method => Update product
57
    updateProduct(id: string, product: any): Observable<Product> {
58
        return this.http.put<Product>(this.apiURL + 'products/' + id +"?API_KEY="+environment.api_key, JSON.stringify(product), this.httpOptions)
59
        .pipe(
60
            retry(1),
61
            catchError(this.handleError)
62
        )
63
    }
64
65
    // HttpClient API delete() method => Delete product
66
    deleteProduct(idProduct: string){
67
        // private baseUrl = `${environment.api+"products"+"?API_KEY="+environment.api_key}`;
68
        // this.baseUrl+"&id="+product.idProduct;
69
        return this.http.delete<Product>(this.apiURL + 'products/' + "?API_KEY=" + environment.api_key +"&id=" + idProduct, this.httpOptions)
70
        .pipe(
71
            retry(1),
72
            catchError(this.handleError)
73
        )
74
    }
75
76
  // Error handling
77
    handleError(error: { error: { message: string; }; status: any; message: any; }) {
78
        let errorMessage = '';
79
        if(error.error instanceof ErrorEvent) {
80
            // Get client-side error
81
            errorMessage = error.error.message;
82
        } else {
83
            // Get server-side error
84
            errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`;
85
        }
86
        window.alert(errorMessage);
87
        return throwError(errorMessage);
88
    }
89
90
}
91